const hasAccess = (access, route) => {
    if (route.meta && route.meta.permission) return hasOneOf(access, route.meta.permission)
    else return true
}

const showThisMenuEle = (item, access) => {
    if (item.meta && item.meta.permission) {
        if (hasOneOf(item.meta.permission, access)) return true
        else return false
    } else return true
}

/**
 * @param {Array} target 目标数组
 * @param {Array} arr 需要查询的数组
 * @description 判断要查询的数组是否至少有一个元素包含在目标数组中
 */

export const hasOneOf = (targetarr, arr) => {
    return targetarr.some(_ => arr.indexOf(_) > -1)
}

export const hasChild = item => {
    return item.children && item.children.length !== 0
}

/**
 * @description 根据name/params/query判断两个路由对象是否相等
 * @param {*} route1 路由对象
 * @param {*} route2 路由对象
 */

export const routeEqual = (route1, route2) => {
    // const params1 = route1.params || {}
    // const params2 = route2.params || {}
    // const query1 = route1.query || {}
    // const query2 = route2.query || {}
    // return (route1.name === route2.name) && objEqual(params1, params2) && objEqual(query1, query2)
    return route1.name === route2.name
}
/**
 * @param {Array} routers 路由列表数组
 * @description 用于找到路由列表中name为home的对象
 */
export const getHomeRoute = (routers, homeName = "home") => {
    let i = -1
    let len = routers.length
    let homeRoute = {}
    while (++i < len) {
        let item = routers[i]
        if (item.children && item.children.length) {
            let res = getHomeRoute(item.children, homeName)
            if (res.name) return res
        } else {
            if (item.name === homeName) homeRoute = item
        }
    }
    return homeRoute
}

/**
 * @param {Array} list 标签列表
 * @param {String} name 当前关闭的标签的name
 */
export const getNextRoute = (list, route) => {
    let res = {}
    if (list.length === 2) {
        res = getHomeRoute(list)
    } else {
        const index = list.findIndex(item => routeEqual(item, route))
        if (index === list.length - 1) res = list[list.length - 2]
        else res = list[index + 1]
    }
    return res
}

/**
 * @returns {Array} 其中的每个元素只包含路由原信息中的name, path, meta三项
 */
export const getTagNavListFromLocalstorage = () => {
    const list = localStorage.tagNaveList
    return list ? JSON.parse(list) : []
}
/**
 * @description 本地存储和获取标签导航列表
 */
export const setTagNavListInLocalstorage = list => {
    localStorage.tagNaveList = JSON.stringify(list)
}
export const showTitle = item => {
    return (item.meta && item.meta.title) || item.name
}
export const getMenuByRouter = (list = [], access = []) => {
    let res = []
    list.length && list.forEach(item => {
        if (!item.meta || (item.meta && !item.meta.hideInMenu)) {
            let obj = {
                path: item.path,
                name: item.name,
                meta: item.meta || {}
            }
            if ((hasChild(item) || item.meta && showThisMenuEle(item, access))) {
                obj.children = getMenuByRouter(item.children, access)
            }
            if (item.meta && item.meta.href) obj.href = item.meta.href
            if (showThisMenuEle(item, access)) res.push(obj)
        }
    })
    return res
}
